//
//  UIDevice+Extension.m
//  iOSShare
//
//  Created by wujin on 13-5-7.
//  Copyright (c) 2013年 wujin. All rights reserved.
//

#import "UIDevice+KZExtension.h"
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_dl.h>
#include "OpenUDID.h"

#import "NSString+KZExtension.h"

#import <CommonCrypto/CommonDigest.h>
//#import <AdSupport/AdSupport.h>

BOOL DeviceSystemSmallerThan(float version)
{
	static float flag=-1;
	if (flag==-1) {
		flag=[[[UIDevice currentDevice] systemVersion] floatValue];
	}
	return flag<version;
}
BOOL DeviceIsiPhone5OrLater()
{
	static int flag=-1;
	if (flag==-1) {
		flag=[UIScreen mainScreen].bounds.size.height>480;
	}
	return flag;
}
BOOL DeviceIsiPad()
{
	__block BOOL isipad=NO;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		isipad=[UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad;
	});
	return isipad;
}

BOOL DeviceIsiPhone()
{
	__block BOOL isiphone=NO;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		isiphone=[UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPhone;
	});
	return isiphone;
}

UIKIT_EXTERN BOOL DeviceIsLarge()
{
    static int flag=-1;
    if (flag==-1) {
        flag=[UIScreen mainScreen].bounds.size.width>320;
    }
    return flag;
}

UIKIT_EXTERN NSUInteger DeviceSystemMajorVersion() {
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _deviceSystemMajorVersion = [[[UIDevice currentDevice] systemVersion] intValue];
    });
    
    return _deviceSystemMajorVersion;
}

UIKIT_EXTERN NSString* mydeviceUniqueIdentifier()
{
    static  NSString *uniqueIdentifier = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        uniqueIdentifier = [UIDevice uniqueOpenIdentifier];
    });
    
    return uniqueIdentifier;
}

@implementation UIDevice (KZExtension)

+ (NSString*) uniqueOpenIdentifier
{
    NSString* uniqueIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"device_slot_open_udid_kz"];
    if (!uniqueIdentifier) {
        uniqueIdentifier = [OpenUDID value];
        if (uniqueIdentifier==nil) {
            uniqueIdentifier = @"0000";
        }
        uniqueIdentifier = [uniqueIdentifier md5DecodingString];
        
        [[NSUserDefaults standardUserDefaults] setObject:uniqueIdentifier forKey:@"device_slot_open_udid_kz"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    return uniqueIdentifier;
}


//+ (NSString*) uniqueIDFAIdentifier
//{
//    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
//    
//}

@end
